package connectivity;

import java.sql.*;

public class Dbmanager {
	public static final String JDBC_EXCEPTION = "JDBC Exception: ";
	public static final String SQL_EXCEPTION = "SQL Exception: ";

	public Connection connection;

	/**
	 * Open database connection
	 */
	public void openConnection() {
		try {
			Class.forName("com.mysql.jdbc.Driver");
			
			String url = "jdbc:mysql://localhost/outfit4you";
			String user = "root", pass = "";

			//String url = "jdbc:mysql://localhost/outfit4you";
			//String user = "root", pass = "";
			//String url = "jdbc:mysql://50.57.185.76/wouter_outfit";
			//String user = "wouter", pass = "8FZbqjntyFtEnC2W";

			/** Open connection */
			connection = DriverManager.getConnection(url, user, pass);
		} catch (ClassNotFoundException e) {
			System.err.println(JDBC_EXCEPTION + e);
		} catch (java.sql.SQLException e) {
			System.err.println(SQL_EXCEPTION + e);
		}
	}

	/**
	 * Close database connection
	 */
	public void closeConnection() {
		try {
			connection.close();
		} catch (Exception e) {
			System.err.println(e.getMessage());
		}
	}

	/**
	 * Executes a query without result.
	 * @param query, the SQl query
	 */
	public void executeQuery(String query) {
		try {
			Statement statement = connection.createStatement();
			statement.executeQuery(query);
		} catch (java.sql.SQLException e) {
			System.err.println(SQL_EXCEPTION + e);
		}
	}

	/**
	 * Executes a query with result.
	 * @param query, the SQL query
	 */
	public ResultSet doQuery(String query) {
		ResultSet result = null;
		try {
			Statement statement = connection.createStatement();
			result = statement.executeQuery(query);
		} catch (java.sql.SQLException e) {
			System.err.println(SQL_EXCEPTION + e);
		}
		return result;
	}
	
	/**
	 * Executes a query with result.
	 * @param query, the SQL query
	 */
	public ResultSet insertQuery(String query) {
		ResultSet result = null;
		try {
			Statement statement = connection.createStatement();
			statement.executeUpdate(query, Statement.RETURN_GENERATED_KEYS);
			result = statement.getGeneratedKeys();
		} catch (java.sql.SQLException e) {
			System.err.println(SQL_EXCEPTION + e);
		}
		return result;
	}

}
